// pages/user/login/login.js
import {
  onlineOrderList,
  cancelOrder,
  confirmOrder
} from '../../../api.js'
var app = getApp(); //获取App内部的信息
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkedId: 0,
    orderStatus: [{
      id: 0,
      value: '全部订单',
      type: ''
    },
    {
      id: 1,
      value: '待付款',
      type: 1
    },
    {
      id: 2,
      value: '待发货',
      type: 2
    },
    {
      id: 3,
      value: '待收货',
      type: 3
    },
    {
      id: 4,
      value: '已完成',
      type: 6
    }
    ],
    orderList: [], //门店订单列表
    typenum: 1,
    last_page: '',
    tab_type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    onlineOrderList({
      token: app.globalData.userInfo.token,
      page: that.data.typenum,
      type: 4
    }).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        that.setData({
          orderList: res.data.data.data,
          last_page: res.data.data.last_page
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('触底了')
    var that = this
    that.setData({
      typenum: that.data.typenum + 1
    })
    if (that.data.typenum > that.data.last_page) {
      wx.showToast({
        title: '暂无更多订单',
        icon: 'none'
      })
    } else {
      onlineOrderList({
        token: app.globalData.userInfo.token,
        page: that.data.typenum,
        type: 4,
        status: that.data.tab_type
      }).then(res => {
        console.log(res)
        if (res.data.code == 666) {
          var lis = that.data.orderList.concat(res.data.data.data)
          that.setData({
            orderList: lis,
          })
        } else if (res.data.code == 111111) {
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/user/login/login',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
    }
  },
  checkedStatus(e) {
    wx.showLoading({
      title: '正在查询',
    })
    var that = this
    this.setData({
      checkedId: e.currentTarget.dataset.id,
      typenum: 1,
      tab_type: e.currentTarget.dataset.type
    })
    onlineOrderList({
      token: app.globalData.userInfo.token,
      page: that.data.typenum,
      type: 4,
      status: that.data.tab_type
    }).then(res => {
      if (res.data.code == 666) {
        wx.hideLoading()
        that.setData({
          orderList: res.data.data.data,
          last_page: res.data.data.last_page
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  // btn_lis: function (e) {
  //   var num = e.currentTarget.dataset.id;
  //   wx.navigateTo({
  //     url: '/pages/user/myOrder/myOrderlis/myOrderlis?id=' + num,
  //   })
  // },
  setText: function (e) {
    console.log(e.currentTarget.dataset.text)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        })
      }
    })
  },
  goBuy: function (e) {
    var num = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/public/payment/payment?order=' + num + '&type=2',
    })
  },
  cancelOrder: function (e) {
    var batchcode = e.currentTarget.dataset.batchcode;
    var index = e.currentTarget.dataset.index;
    var that = this
    cancelOrder({
      token: app.globalData.userInfo.token,
      batchcode: batchcode
    }).then(res => {
      if (res.data.code == 666) {
        wx.showToast({
          title: '取消成功',
        })
        console.log(that.data.orderList)
        console.log(that.data.checkedId)
        if (that.data.checkedId == 1) {
          that.data.orderList.splice(index, 1)
          that.setData({
            orderList: that.data.orderList
          })
        } else if (that.data.checkedId == 0) {
          that.data.orderList[index].product_data[0].sales_value = "交易关闭"
          that.setData({
            orderList: that.data.orderList
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  confirm: function (e) {
    var batchcode = e.currentTarget.dataset.batchcode;
    var index = e.currentTarget.dataset.index;
    var that = this
    confirmOrder({
      token: app.globalData.userInfo.token,
      batchcode: batchcode
    }).then(res => {
      if (res.data.code == 666) {
        wx.showToast({
          title: '收货成功',
        })
        console.log(that.data.orderList)
        console.log(that.data.checkedId)
        if (that.data.checkedId == 3) {
          that.data.orderList.splice(index, 1)
          that.setData({
            orderList: that.data.orderList
          })
        } else if (that.data.checkedId == 0) {
          that.data.orderList[index].product_data[0].sales_value = "已完成"
          that.setData({
            orderList: that.data.orderList
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  }
})